package com.example.provider.controller;

import com.example.common.entity.Bidding;
import com.example.common.entity.Demand;
import com.example.common.entity.Message;
import com.example.common.entity.WorkRoom;
import com.example.provider.service.WorkroomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@Api(value="工作室controller",tags={"工作室相关接口"})
@RestController
@RequestMapping("/workroom")
public class WorkroomController {
    @Autowired
    private WorkroomService workroomService;

    /**
     * 工作室注册
     * @param workRoom
     * @return
     */
    @CrossOrigin(origins = "*",maxAge = 3600)
    @ResponseBody
    @PostMapping("/workroomApply")
    public Message workroomApply(@RequestBody WorkRoom workRoom){
        return workroomService.applyWorkroom(workRoom);
    }

    /**
     * 根据工作室id搜索对应的竞标情况
     * @param id 当前登录用户id
     * @return
     */
    @GetMapping("/checkBidding")
    public List<Demand> checkBidding(int id) throws IOException {
        return workroomService.checkBidding(workroomService.searchWIdById(id));
    }

    /**
     * 删除竞标情况通过需求id(组号)，工作室id
     * @param bidding 这里传来两个值groupId和workroomId(现在是当前用户id)
     * @return
     */
    @PostMapping("/deleteBidding")
    public Message deleteBidding(@RequestBody Bidding bidding){
        //先取出真正的工作室id
        int workroomId = workroomService.searchWorkroomByIdd(bidding.getWorkroomId()).getId();
        return workroomService.deleteBidding(workroomId, bidding.getGroupId());
    }

    /**
     * 通过当前用户id取出工作室
     * @param idd 当前用户id
     * @return
     */
    @GetMapping("/searchByIdd")
    public WorkRoom searchByIdd(int idd){
        return workroomService.searchWorkroomByIdd(idd);
    }

    /**
     * 先给工作室对于这次竞标评个分
     * 然后从前端收到的公司id，组号，工作室id，资金等信息存入竞标关系中
     * 这里组号应于发布需求时的需求id结合起来
     * @param bidding 竞标关系
     * @return
     */
    @PostMapping("/applyDemand")
    public Message applyDemand(@RequestBody Bidding bidding) throws IOException {
        bidding.setGrade(workroomService.getGrade(bidding));
        return workroomService.addBidding(bidding);
    }

    @ApiOperation("根据工作室id查看工作室信息")
    @GetMapping("/checkById")
    public WorkRoom checkById(int id) {
        return workroomService.checkById(id);
    }
}
